package com.auto.learning.activity;

import java.util.ArrayList;
import java.util.List;

import com.auto.learning.MainActivity;
import com.auto.learning.R;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.view.SharedPreferencesUtils;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.view.LayoutInflater;
import android.view.View;

import android.view.Window;
import android.widget.ImageView;


//第一次运行的引导页代码
public class WelcomeActivity extends Activity implements OnPageChangeListener{

	private ViewPager viewPager;
	private PagerAdapter pagerAdapter;
	private ArrayList<View> views;
	private int[] images;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome1);
		// 设置引导图片
		images = new int[] { R.drawable.welcome1, R.drawable.welcome2, R.drawable.welcome3, R.drawable.welcome4};
		initView();
	}
	//引导页使用的pageview适配器
	 class BasePagerAdapter extends PagerAdapter {
		private List<View> views = new ArrayList<View>();

		public BasePagerAdapter(List<View> views2) {
			this.views = views2;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(views.get(position));
		}

		@Override
		public float getPageWidth(int position) {
			// TODO Auto-generated method stub
			return super.getPageWidth(position);
		}

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(views.get(position));
			return views.get(position);
		}
	}

	// 初始化视图
	private void initView() {
		SharedPreferencesUtils.setParam(this, "isFirst", false);
		// 实例化视图控件
		viewPager = (ViewPager) findViewById(R.id.viewpage);
		views=new	ArrayList<View>();
		for (int i = 0; i < images.length; i++) {
			View view1 = LayoutInflater.from(WelcomeActivity.this).inflate(R.layout.guidanceone1, null);
			ImageView image=(ImageView) view1.findViewById(R.id.imageView1);
			image.setImageBitmap(CommonMethod.readBitMap(WelcomeActivity.this, images[i]));
			if(i == images.length-1){
				image.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						/*SharedPreferences shared =getSharedPreferences("first", Context.MODE_PRIVATE);
						Editor editor = shared.edit();
						editor.putBoolean("First", false);
						editor.commit();
						startActivity(new Intent(WelcomeActivity.this,SmoothActivity.class));
						WelcomeActivity.this.finish();*/
						
						Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
						startActivity(intent);
						finish();
					}
				});
			}
			views.add(view1);
		}
		pagerAdapter = new BasePagerAdapter(views);
		viewPager.setAdapter(pagerAdapter); // 设置适配器
		viewPager.setOnPageChangeListener(this);
	}


	boolean isScrolled = false;

	@Override
	public void onPageScrollStateChanged(int status) {
		// TODO Auto-generated method stub

		switch (status) {
		case 1:// 手势滑动
			isScrolled = false;
			break;
		case 2:// 界面切换
			isScrolled = true;
			break;
		case 0:// 滑动结束
			// 当前为最后一张，此时从右向左滑，则切换到第一张
			/*if (viewPager.getCurrentItem() == viewPager.getAdapter().getCount() - 1 && !isScrolled) {
				SharedPreferences shared =getSharedPreferences("first", Context.MODE_PRIVATE);
				Editor editor = shared.edit();
				editor.putBoolean("First", false);
				editor.commit();
				startActivity(new Intent(WelcomeActivity.this,SmoothActivity.class));
				WelcomeActivity.this.finish();

			}*/
			break;
		}
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}
	
	@Override
	public void onPageSelected(int arg0) {
		
	}
}
